//
//  GYCartConfirmViewModel.m
//  GYDemo
//
//  Created by mac on 2018/4/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYCartConfirmViewModel.h"
#import "Head.h"

@implementation GYCartConfirmModel

@end

@interface GYCartConfirmViewModel()
@property (nonatomic,strong) NSString *serverCost;
@property (nonatomic,strong) NSString *payCost;
@end

@implementation GYCartConfirmViewModel
#pragma mark - getter and setter
-(NSMutableArray *)datas
{
    if (!_datas) {
        _datas = [NSMutableArray new];
    }
    return _datas;
}
-(GYMineShopAddressModel *)addressModel
{
    GYMineShopAddressViewModel *addressVM = [GYMineShopAddressViewModel shareInterface];
    return [addressVM defaultAddress];
}
#pragma mark - method
-(NSString *)finalPrice
{
    return [NSString stringWithFormat:@"合计：%.2f元",_payCost.floatValue];
}
-(NSString *)serverPrice
{
    return [NSString stringWithFormat:@"服务费：%.2f元",@"20"];
}
//shoplist
-(NSString *)getShopList
{
    NSString *res = @"";
    NSMutableArray *marr = [NSMutableArray new];
    for (GYCartShopModel *model in self.datas) {
        NSString *idd = [NSString stringWithFormat:@"%ld",model.shopcarid];
        NSString *remark = model.comment ?: @"";
        NSDictionary *dic = @{@"id":idd,@"remark":remark};
        [marr addObject:dic];
    }
    NSData *data;
    if (@available(iOS 11.0, *)) {
        data = [NSJSONSerialization dataWithJSONObject:marr options:NSJSONWritingPrettyPrinted | NSJSONWritingSortedKeys error:nil];
    } else {
        data = [NSJSONSerialization dataWithJSONObject:marr options:NSJSONWritingPrettyPrinted error:nil];
    }
    res = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    return res;
}

-(void)requestDatas
{
    @WeakSelf;
    NSString *interface = @"newShopCarConfirm";
    NSMutableDictionary *para = [NSMutableDictionary new];
    NSString *shopcarids = [self.carViewModel shopcarids];
    [para setObject:shopcarids forKey:@"shopcarids"];
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        if (0 == infoModel.status) {
            NSDictionary *data = infoModel.data;
            if ([data isKindOfClass:[NSDictionary class]]) {
                NSArray *datalist = data[@"datalist"];
                if ([datalist isKindOfClass:[NSArray class]]) {
                    NSMutableArray *arr = [NSMutableArray new];
                    for (NSDictionary *dic in datalist) {
                        GYCartShopModel *model = [GYCartShopModel yy_modelWithJSON:dic];
                        model.is_selected = YES;
                        [arr addObject:model];
                    }
                    NSString *fuwufei = data[@"fuwufei"] ?: @"";
                    _serverCost = fuwufei;
                    NSString *payamount = data[@"payamount"] ?: @"";
                    _payCost = payamount;
                    [selfp setDatas:arr];
                }
            }
        }else{
            NSMutableArray *arr = [NSMutableArray new];
            [selfp setDatas:arr];
        }
    }];
}

///**
// *      提交订单接口 - bz 1-来自购物车，2-来自立即下单
// *       1.bz为1时只需要传留言内容
// *       2.bz为2时id、数量、价格、留言都需要传
// */
//-(void)requestConfirm
//{
//    @WeakSelf;
//    NSMutableDictionary *para = [NSMutableDictionary new];
//    [para setObject:@"1" forKey:@"bz"];
//    for (GYCartConfirmModel *model in selfp.datas) {
//        NSString *companyname  = model.companyname;
//        NSString *comment = model.comment ?: @"";
//        [para setObject:comment forKey:companyname];
//    }
//    NSString *interface = @"submitOrder";
//    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
//        [[GYCartViewModel shareInterface]requestDatas];
//        selfp.isComfired = @"true";
//    }];
//}
-(void)requestConfirm:(NSMutableDictionary *)para
{
    @WeakSelf;
    NSString *interface = @"addUVTOrder";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [[GYCartViewModel shareInterface]requestDatas];
        selfp.isComfired = @"true";
    }];
}
#pragma mark - life
@end
